package com.zyw.zhuangdian.service.impl;

import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.dao.ProductionDao;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.pojo.in.ProductionDtoIn;
import com.zyw.zhuangdian.pojo.out.ProductionDtoOut;
import com.zyw.zhuangdian.property.MyProperties;
import com.zyw.zhuangdian.service.ProductionService;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProductionServiceImpl implements ProductionService {
    private final Logger logger = LoggerFactory.getLogger(ProductionServiceImpl.class);

    @Autowired
    private ProductionDao productionDao;

    @Autowired
    private MyProperties properties;

    @Override
    public Resp<Void> addProduction(ProductionDtoIn in) {
        Resp<Void> resp = new Resp<>();

        productionDao.addProduction(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> updateProduction(ProductionDtoIn in) {
        Resp<Void> resp = new Resp<>();

        productionDao.updateProduction(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> deleteProduction(ProductionDtoIn in) {
        Resp<Void> resp = new Resp<>();

        in.setStatus(ServiceConstant.STATUS_INVALID);

        productionDao.updateProduction(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<List<ProductionDtoOut>> listProductions(ProductionDtoIn in) {
        Resp<List<ProductionDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }

        if (!CollectionUtils.isEmpty(in.getFitSkins())){
            in.setForSkin(String.join("%",in.getFitSkins()));
        }

        int count = productionDao.listProductionsCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0){
            List<Map> users = productionDao.listProductions(BeanConvertUtil.beanCovertMap(in));
            List<ProductionDtoOut> dtoOuts = BeanConvertUtil.covertBeanList(users, ProductionDtoOut.class);
            dtoOuts.forEach(item->{
                if (StringUtils.isNotBlank(item.getPictures())){
                    List<String> picList = new ArrayList<>();
                    String pics = item.getPictures();
                    String[] strings = pics.split(",");
                    for(String pic :strings){
                        if (!",".equals(pic)){
                            picList.add(pic);
                        }
                    }
                    item.setPictureList(picList);
                }
            });
            resp.setData(dtoOuts);
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }
        return resp;
    }
}
